package com.apex.lottery.service.strategy.activitystatus.operater;

import com.apex.lottery.dao.dataobject.ActivityDO;
import com.apex.lottery.dao.mapper.ActivityMapper;
import com.apex.lottery.dao.mapper.ActivityPrizeMapper;
import com.apex.lottery.service.dto.ConvertActivityStatusDTO;
import com.apex.lottery.service.enums.ActivityPrizeStatusEnum;
import com.apex.lottery.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName ActivityOperator
 * @Description
 * @Author ZJX
 * @Date 2025/4/21 12:13
 * @Version 1.0
 **/
@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
//        判断是否奖品已经全部抽取完成
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if (activityId == null || targetActivityStatus == null){
            return false;
        }

        ActivityDO activityDO = activityMapper.selectById(activityId);
        if (activityDO == null){
            return false;
        }
        if (activityDO.getStatus().equalsIgnoreCase(targetActivityStatus.name())){
            return false;
        }
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());

        return count <= 0;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
